<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="UTF-8">
        <title>更新页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/component/jsoneditor/css/jsoneditor.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
        <style>
            .selected_class{
                box-shadow: 3px 3px 6px rgba(85, 154, 227, 0.4);
                border: 2px solid #2d8cf0;
            }
            .ellipsis {
                overflow: hidden; /* 确保超出容器的文本被隐藏 */
                white-space: nowrap; /* 确保文本在一行内显示 */
                text-overflow: ellipsis; /* 使用省略号表示被截断的文本 */
            }
        </style>
    </head>
    <body>

        <form class="layui-form">

            <div class="mainBox">
                <div class="main-container mr-5">
                    <div class="layui-form-item">
                        <label class="layui-form-label required">商家</label>
                        <div class="layui-input-block">
                            <div name="business_id" id="business_id" value=""></div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">标题</label>
                        <div class="layui-input-block">
                            <input type="text" name="title" value=""  lay-verify="required" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">封面图</label>
                        <div class="layui-input-block">
                            <img class="img-3" src=""/>
                            <input type="text" style="display:none" name="main_image" value=""  lay-verify="required"/>
                            <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="main_image"  permission="app.admin.upload.image">
                                <i class="layui-icon layui-icon-upload"></i>上传图片
                            </button>
                            <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="attachment-choose-image_path"  permission="app.admin.upload.attachment">
                                <i class="layui-icon layui-icon-align-left"></i>选择图片
                            </button>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label required">上线时间</label>
                        <div class="layui-input-block">
                            <input  type="text" name="start_time" id="start_time" autocomplete="off" lay-verify="required" class="layui-input" >
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">标签</label>
                        <div class="layui-input-block">
                            <input type="text" name="labels" placeholder="请输入标签" class="layui-input">
                            <tip>多个用英文逗号,隔开</tip>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">自定义标签</label>
                        <div class="layui-input-block">
                            <input type="text" name="custom_tags" placeholder="请输入标签" class="layui-input">
                            <tip>多个用英文逗号,隔开</tip>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">简介</label>
                        <div class="layui-input-block">
                            <textarea name="desc" class="layui-textarea" rows="8"></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item ai_image_command_item">
                        <label class="layui-form-label required">AI生图指令模板</label>
                        <div class="layui-input-block ai_image_command_html"  style="margin-right:10px;display: flex;flex-direction: row;align-items: center;flex-wrap: wrap;">
                            <div class="font_click"  data-id="1" style="width: 100px;height: 120px;margin-right: 10px;">
                                <img src="https://ai-video-1320718644.cos.ap-guangzhou.myqcloud.com/video/local/stable_diffusion_image/20240513/png/0b52503b7725942befdf.png" style="width: 100px;height: 100px;">
                                <div>实打实大苏打</div>
                            </div>
                        </div>
                        <input type="hidden" name="ai_image_command_id" value="" lay-verify="required">
                    </div>
                </div>
            </div>

            <div class="bottom">
                <div class="button-container">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit="" lay-filter="save">
                        提交
                    </button>
                    <!--<button type="reset" class="pear-btn pear-btn-md">-->
                        <!--重置-->
                    <!--</button>-->
                </div>
            </div>
            
        </form>

        <script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/component/jsoneditor/jsoneditor.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        <script src="/app/admin/admin/js/functions.js"></script>
        <script src="/app/admin/admin/js/md5.js"></script>
        
        <script>
            // 相关接口
            const PRIMARY_KEY = "id";
            const PRIMARY_KEY_VALUE = layui.url().search[PRIMARY_KEY];
            const SELECT_API = "/app/admin/novel/index/select" + location.search;
            const UPDATE_API = "/app/admin/novel/index/update";


            // 获取数据库记录
            layui.use(["form", "util", "popup"], function () {
                $ = layui.$;
                $.ajax({
                    url: SELECT_API,
                    dataType: "json",
                    async:true,
                    headers: get_header({"id":PRIMARY_KEY_VALUE}),
                    success: function (res) {
                        
                        // 给表单初始化数据
                        layui.each(res.data[0], function (key, value) {
                            let obj = $('*[name="'+key+'"]');
                            if (key === "password") {
                                obj.attr("placeholder", "不更新密码请留空");
                                return;
                            }
                            if (typeof obj[0] === "undefined" || !obj[0].nodeName) return;
                            if (obj[0].nodeName.toLowerCase() === "textarea") {
                                obj.val(value);
                            } else {
                                obj.attr("value", value);
                                obj[0].value = value;
                            }
                        });
                        
                        // 字段 图片地址 image_path
                        layui.use(["upload", "layer"], function() {
                            let input = layui.$("#main_image").prev();
                            input.prev().attr("src", input.val());
                            layui.$("#attachment-choose-image_path").on("click", function() {
                                parent.layer.open({
                                    type: 2,
                                    title: "选择附件",
                                    content: "/app/admin/upload/attachment?ext=jpg,jpeg,png,gif,bmp",
                                    area: ["95%", "90%"],
                                    success: function (layero, index) {
                                        parent.layui.$("#layui-layer" + index).data("callback", function (data) {
                                            input.val(data.url).prev().attr("src", data.url);
                                        });
                                    }
                                });
                            });
                            layui.upload.render({
                                elem: "#main_image",
                                acceptMime: "image/gif,image/jpeg,image/jpg,image/png",
                                url: "/app/admin/upload/image",
                                field: "__file__",
                                headers: get_header({}),
                                done: function (res) {
                                    if (res.code > 0) return layui.layer.msg(res.msg);
                                    this.item.prev().val(res.data.url).prev().attr("src", res.data.url);
                                }
                            });
                        });

                        // 字段 商家 business_id
                        layui.use(["jquery", "xmSelect", "popup"], function() {
                            layui.$.ajax({
                                url: "/app/admin/novel/business/select?format=select&limit=1000",
                                dataType: "json",
                                async:true,
                                headers: get_header({"format":"select"}),
                                success: function (res) {
                                    let value = layui.$("#business_id").attr("value");
                                    let initValue = value ? value.split(",") : [];
                                    layui.xmSelect.render({
                                        el: "#business_id",
                                        name: "business_id",
                                        filterable: true,
                                        initValue: initValue,
                                        data: res.data,
                                        model: {"icon":"hidden","label":{"type":"text"}},
                                        clickClose: true,
                                        radio: true,
                                    })
                                    if (res.code) {
                                        layui.popup.failure(res.msg);
                                    }
                                }
                            });
                        });

                        layui.use(["laydate"], function() {
                            let value = layui.$("#start_time").attr("value");
                            layui.laydate.render({
                                elem: "#start_time",
                                type: "datetime",
                                mark:date_today_mark,
                                value:value
                            });
                        })

                        if(res.data[0]['spread_type']<3){
                            layui.$.ajax({
                                url: "/app/admin/image/aiimagecommand/select?status=1&limit=100",
                                dataType: "json",
                                async:true,
                                headers: get_header({"status":1}),
                                success: function (res2) {
                                    let html = '';
                                    layui.$.each(res2.data,function (i,v) {
                                        let selected = '';
                                        if(res.data[0]['ai_image_command_id']==v.id){
                                            selected = 'selected_class';
                                        }
                                        html += ' <div class="command_click '+selected+'" data-id="'+v.id+'" style="width: 100px;height: 120px;margin-right: 10px;">\n' +
                                            '           <img src="'+v.image+'" style="width: 100px;height: 100px;">\n' +
                                            '           <div class="ellipsis">'+v.name+'</div>\n' +
                                            '  </div>';
                                    })
                                    layui.$('.ai_image_command_html').html(html);
                                    if (res2.code) {
                                        layui.popup.failure(res2.msg);
                                    }
                                }
                            });
                            $('.ai_image_command_html').delegate('.command_click','click',function () {
                                $('.command_click').removeClass('selected_class');
                                $(this).addClass('selected_class');
                                $('input[name="ai_image_command_id"]').val($(this).data('id'));
                            })
                        }else{
                            $('.ai_image_command_item').hide();
                        }

                        // ajax返回失败
                        if (res.code) {
                            layui.popup.failure(res.msg);
                        }
                    }
                });
            });

            //提交事件
            layui.use(["form", "popup"], function () {
                layui.form.on("submit(save)", function (data) {
                    data.field[PRIMARY_KEY] = layui.url().search[PRIMARY_KEY];
                    layui.$.ajax({
                        url: UPDATE_API,
                        type: "POST",
                        dateType: "json",
                        data: data.field,
                        async:true,
                        headers: get_header(data.field),
                        success: function (res) {
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                parent.refreshTable();
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });
                        }
                    });
                    return false;
                });
            });

        </script>

    </body>

</html>
